
#import "TBSong.h"


@implementation TBSong

@synthesize songTitle;
@synthesize artist;
	
-(void)encodeWithCoder:(NSCoder*)coder
{
	[coder encodeObject:songTitle];
	[coder encodeObject:artist];
}

-(id)initWithCoder:(NSCoder*)coder
{
	if (self = [super init])
	{
		[self setSongTitle:[coder decodeObject]];
		[self setArtist:[coder decodeObject]];
	}
}

-(void)dealloc {
	[songTitle release];
	[artist release];
	[super dealloc];
}

+ (TBSong *) songWithTitle:(NSString *)theTitle andArtist:(NSString *)theArtist {
	return [[[TBSong alloc] initWithTitle:theTitle andArtist:theArtist] autorelease];
}

- (TBSong *) initWithTitle:(NSString *)theTitle andArtist:(NSString *)theArtist
{
	if (self = [super init])
	{
		[self setSongTitle:theTitle];
		[self setArtist:theArtist];
		return self;
	}
}

- (NSString *) description
{
	return [NSString stringWithFormat:@"%@ - %@", [self artist], [self songTitle]];
}

-(BOOL) isEqual:(id)other
{
	return [self isEqualToSong:other];
}

-(NSUInteger) hash {
	return [[self artist] hash] + [[self songTitle] hash];
}

-(BOOL)isEqualToSong:(TBSong *)otherSong 
{
	if ([self hash] == [otherSong hash])
		return YES;
	
	if (![otherSong respondsToSelector:@selector(artist)] || ![otherSong respondsToSelector:@selector(songTitle)])
	{
		NSLog(@"Warning: [TBSong isEqualToSong:] called with non-TBSong argument");
		return NO;
	}

//	if ([[self artist] isEqualToString:[otherSong artist]] && [[self songTitle] isEqualToString:[otherSong songTitle]])
	if ([[self artist] 	  compare:[otherSong artist] 	options:NSCaseInsensitiveSearch] == NSOrderedSame &&
		[[self songTitle] compare:[otherSong songTitle] options:NSCaseInsensitiveSearch] == NSOrderedSame)
	{
		return YES;
	}
	else
	{
		return NO;
	}
}
@end
